package com.example.test_1_21.mqserver.core;

import java.io.Serializable;
import java.util.Arrays;
import java.util.UUID;

//表示一个要传递的消息
public class Message implements Serializable {
    //Message最核心的部分
    private BasicProperties basicProperties=new BasicProperties();
    private byte[] body;

    //辅助属性：[offsetBeg,offsetEnd)
    //消息数据的开头距离文件开头的位置偏移（字节）
    private transient long offsetBeg=0;
    //消息数据的结尾距离文件开头的距离偏移（字节）
    private transient long offsetEnd=0;
    //表示消息在文件中是否有效（针对文件中的消息，如果删除，使用逻辑删除）
    private byte isValid=0x1;   //0x1表示有效，0x0表示无效

    //创建一个工厂方法，来封装一下，创建的Message对象的过程
    public static Message createMessageWithId(String routingKey,BasicProperties basicProperties,byte[] body){
        Message message=new Message();
        if(basicProperties!=null){
            message.setBasicProperties(basicProperties);
        }
        //此处生成的MessageId以M-作为前缀
        message.setMessageId("M-"+UUID.randomUUID());
        message.basicProperties.setRoutingKey(routingKey);
        message.body=body;
        //此处只设置核心属性，其他属性在消息持久化时设置
        return message;
    }

    public String getMessageId(){
        return basicProperties.getMessageId();
    }

    public void setMessageId(String messageId){
        basicProperties.setMessageId(messageId);
    }

    public String getRoutingKey(){
        return basicProperties.getRoutingKey();
    }

    public void setRoutingKey(String routingKey){
        basicProperties.setRoutingKey(routingKey);
    }

    public int getDeliverMode(){
        return basicProperties.getDeliverMode();
    }

    public void setDeliverMode(int mode){
        basicProperties.setDeliverMode(mode);
    }

    public BasicProperties getBasicProperties() {
        return basicProperties;
    }

    public void setBasicProperties(BasicProperties basicProperties) {
        this.basicProperties = basicProperties;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public long getOffsetBeg() {
        return offsetBeg;
    }

    public void setOffsetBeg(long offsetBeg) {
        this.offsetBeg = offsetBeg;
    }

    public long getOffsetEnd() {
        return offsetEnd;
    }

    public void setOffsetEnd(long offsetEnd) {
        this.offsetEnd = offsetEnd;
    }

    public byte getIsValid() {
        return isValid;
    }

    public void setIsValid(byte isValid) {
        this.isValid = isValid;
    }

    @Override
    public String toString() {
        return "Message{" +
                "basicProperties=" + basicProperties +
                ", body=" + Arrays.toString(body) +
                ", offsetBeg=" + offsetBeg +
                ", offsetEnd=" + offsetEnd +
                ", isValid=" + isValid +
                '}';
    }
}
